package com.dundun.SSO.manage.impl;

import com.dundun.SSO.core.impl.BaseServiceImpl;
import com.dundun.SSO.manage.ISsoGroupRoleRelationService;
import com.dundun.SSO.manage.ISsoGroupService;
import com.dundun.SSO.manage.ISsoRoleService;
import com.dundun.SSO.manage.ISsoUserGroupRelationService;
import com.dundun.SSO.manage.dao.SsoRoleDao;
import com.dundun.SSO.manage.pojo.SsoGroup;
import com.dundun.SSO.manage.pojo.SsoGroupRoleRelation;
import com.dundun.SSO.manage.pojo.SsoRole;
import com.dundun.SSO.manage.query.SsoRoleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/6 0006 15:16
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Service
@Transactional
public class SsoRoleServiceImpl extends BaseServiceImpl<SsoRole> implements ISsoRoleService {

    @Autowired
    private SsoRoleDao ssoRoleDao;

    @Autowired
    private ISsoGroupService ssoGroupService;

    @Autowired
    private ISsoUserGroupRelationService ssoUserGroupRelationService;

    @Autowired
    private ISsoGroupRoleRelationService ssoGroupRoleRelationService;

    public List<SsoRole> selectByPage(SsoRoleQuery ssoRoleQuery) {
        return ssoRoleDao.selectByPage(ssoRoleQuery);
    }

    public Integer selectCountByPage(SsoRoleQuery ssoRoleQuery) {
        return ssoRoleDao.selectCountByPage(ssoRoleQuery);
    }

    public List<SsoRole> selectAllParentRole(Long parentId) {
        List<SsoRole> ssoRoleList = new ArrayList<SsoRole>();
        do {
            SsoRole ssoRole = ssoRoleDao.selectByPrimaryKey(parentId);
            ssoRoleList.add(ssoRole);
            parentId = ssoRole.getParentId();
        } while (parentId != null);
        return ssoRoleList;
    }

    public List<SsoRole> selectAllParentRole(SsoRole ssoRole) {
        return selectAllParentRole(ssoRole.getParentId());
    }

    public List<SsoRole> selectAllSubRole(Long id) {
        SsoRole ssoRole = new SsoRole();
        ssoRole.setId(id);
        return selectAllSubRole(ssoRole);
    }

    public List<SsoRole> selectAllSubRole(SsoRole ssoRole) {
        List<SsoRole> ssoRoleList = new ArrayList<SsoRole>();
        ssoRoleList.add(ssoRole);

        for (int i = 0; i < ssoRoleList.size(); i++) {
            Example example = new Example(SsoRole.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("parentId", ssoRoleList.get(i).getId());
            List<SsoRole> ssoRoleList1 = ssoRoleDao.selectByExample(example);
            if (ssoRoleList1.size() > 0) {
                ssoRoleList.addAll(ssoRoleList1);
            }
        }
        ssoRoleList.remove(0);
        return ssoRoleList;
    }

    public boolean deleteList(List<Long> idList) {
        int i = 1;
        for (Long id : idList) {
            i *= delete(id);
            if(id == 2){
                throw new RuntimeException();
            }
        }
        if (i == 0) {
            return false;
        }
        return true;
    }

    public List<SsoRole> selectAllRoleByUserId(Long userId) {
        Set<SsoRole> ssoRoleSet = new HashSet<>();
        List<SsoGroup> ssoGroupList = new ArrayList<>();
        ssoGroupList.addAll(ssoGroupService.selectAllGroupByUserId(userId));
        for(SsoGroup ssoGroup:ssoGroupList){
            ssoRoleSet.addAll(selectAllRoleByGroupId(ssoGroup.getId()));
        }
        return new ArrayList<>(ssoRoleSet);
    }

    public List<SsoRole> selectAllRoleByGroupId(Long groupId) {
        List<SsoGroupRoleRelation> ssoGroupRoleRelationList = ssoGroupRoleRelationService.selectAllRelationByGroupId(groupId);
        Set<SsoRole> ssoRoleSet = new HashSet<>();
        for(SsoGroupRoleRelation ssoGroupRoleRelation:ssoGroupRoleRelationList){
            ssoRoleSet.add(ssoRoleDao.selectByPrimaryKey(ssoGroupRoleRelation.getRoleId()));
            ssoRoleSet.addAll(selectAllParentRole(ssoGroupRoleRelation.getRoleId()));
        }
        return new ArrayList<>(ssoRoleSet);
    }
}
